<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShowDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column align="center" type="index" label="序号" width="120" />
              <el-table-column align="center" prop="name" label="名称" width="240" />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column align="center" label="操作">
                <template #default="obj">
                  <el-button size="small" type="success" @click="assginPerm(obj.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(obj.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(obj.row.id)">删除</el-button>
                </template>
              </el-table-column>

            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="end" align="middle" style="height:60px">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址" prop="companyAddress">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱" prop="companyPhone">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注" prop="remarks">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <!-- 角色编辑弹窗 -->
      <el-dialog ref="roleForm" :title="roleForm.id?'编辑角色':'新增角色'" :visible="isShowDialog" destory-on-clos="true" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 权限编辑弹窗 -->
      <el-dialog :visible="isShowPerm" title="权限编辑" @close="permClose">
        <el-tree
          ref="permTree"
          :data="permList"
          default-expand-all
          check-strictly
          :props="{label:'name'}"
          show-checkbox
          node-key="id"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnPermOk">确认</el-button>
            <el-button size="small" @click="permClose">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addRole, delRole, editRole, getCompanyInfo, getRoleDetail, getRoleList, editRolePerm } from '@/api/setting'
import { getPermList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      isShowPerm: false,
      roleId: '',
      permList: [],
      roleList: [],
      companyInfo: {},
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 3,
        total: 0 // 记录总数
      },
      isShowDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo()
    this.getPermList() // 获取权限列表
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.roleList = rows
    },
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
      console.log(94, this.companyInfo)
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList(this.page)
    },
    // 删除角色
    async delRole(id) {
      await this.$confirm('是否确认删除该角色?')
      await delRole(id)
      this.$message.success('删除角色成功!')
      await this.getRoleList()
      // 判断该页是否删完,如果删完了应该页码减一,重新请求数据
      if (Math.ceil((this.page.total / this.page.pagesize)) < this.page.page) {
        if (this.page.page === 1) {
          this.page.page = 1
          this.getRoleList()
        } else {
          this.page.page--
          this.getRoleList()
        }
      }
    },
    // 添加角色
    btnCancel() {
      // 关闭弹窗,清空数据,清空表单验证
      this.isShowDialog = false
      setTimeout(() => {
        this.roleForm = {
          name: '',
          description: ''
        }
        this.$refs.roleForm.resetFields()
      }, 100)
    },
    async btnOK() {
      // 校验
      await this.$refs.roleForm.validate()
      // 发送请求
      if (this.roleForm.id) {
        // 更新
        await editRole(this.roleForm)
      } else {
        await addRole(this.roleForm)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 更新页面
      this.getRoleList()
    },
    // 编辑角色
    async editRole(id) {
      // 数据回显
      this.roleForm = await getRoleDetail(id)
      this.isShowDialog = true
    },
    // 分配权限
    async assginPerm(id) {
      const { permIds } = await getRoleDetail(id)
      this.isShowPerm = true
      this.roleId = id
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    // 回显权限列表
    async getPermList() {
      this.permList = listToTreeData(await getPermList(), '0')
    },
    // 发送请求,修改角色权限
    async btnPermOk() {
      await editRolePerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.$message.success('更新角色权限成功!')
      this.permClose()
    },
    // 关闭权限弹窗
    permClose() {
      this.$refs.permTree.setCheckedKeys([])
      this.isShowPerm = false
    }
  }
}
</script>

<style>

</style>
